package com.ticketbooking.controller.UserManager;

import com.alibaba.fastjson.JSON;
import com.ticketbooking.constant.Constant;
import com.ticketbooking.entity.ManagerEntity;
import com.ticketbooking.entity.ManagerInfEntity;
import com.ticketbooking.entity.UserInfEntity;
import com.ticketbooking.service.IManagerDao;
import com.ticketbooking.service.IManagerInfDao;
import com.ticketbooking.service.IUserInfDao;
import com.ticketbooking.util.SecurityTool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/usermanager")
public class UserManagerController {

    @Resource
    private IUserInfDao userInfDao;

    @Resource
    private IManagerInfDao managerInfDao;

    @Resource
    private IManagerDao managerDao;

    /**
     * localhost:8888/usermanager/getUserData.action
     * @return 所有用户信息，与数据库字段相同,如userid,username等
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getUserData")
    public String getUserData(HttpServletRequest request,Model model){
        List userList = null;
        if(SecurityTool.isReturnJson("1",request)){
            userList = userInfDao.getAllUserInf();
        }
        model.addAttribute("userList",userList);
        return Constant.JSON_VIEW_OTHER;
    }

    /**
     * localhost:8888/usermanager/getManagerInf.action
     * @return 所有用户信息，与数据库字段相同,如userid,username等
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getManagerInf")
    public String getManagerInf(HttpServletRequest request,Model model){
        List managerList = null;
        String manrole = request.getParameter("manrole");
        if(SecurityTool.isReturnJson("2",request)){
            if(manrole!=null){
                managerList = managerInfDao.getManagerInfByRolename(manrole);
            } else{
                managerList = managerInfDao.getManagerInf();
            }
        }
        model.addAttribute("userList",managerList);
        return Constant.JSON_VIEW_OTHER;
    }

    /**
     * localhost:8888/usermanager/getRoleInf.action
     * @return 所有角色信息，rolecode(角色编码),role(角色名称),该角色人数(好像要加个看该角色的用户功能吧)
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getRoleInf")
    public String getRoleInf(HttpServletRequest request,Model model){
        List roleList = null;
        if(SecurityTool.isReturnJson("1",request)){
            roleList = managerInfDao.getRoleInf();
        }
        model.addAttribute("roleList",roleList);
        return Constant.JSON_VIEW_OTHER;
    }

    /**
     * localhost:8888/usermanager/delRoleInf.action 删除角色信息(同时删除该角色的管理用户)
     * @param rolecode 角色编码(rolecode,详细看数据库)
     * @return 返回被删除的管理用户信息
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/delRoleInf")
    public String delRoleInf(HttpServletRequest request, String rolecode, Model model) {
        int result = 0;
        if (SecurityTool.isReturnJson("1", request)) {
            result = managerInfDao.delRoleInf(rolecode);
        }
        model.addAttribute("result", result);
        return Constant.JSON_VIEW_OTHER;
    }

    /**
     * localhost:8888/usermanager/addRoleInf.action 增加角色信息
     * @param rolecode 角色代码 admin/manager之类
     * @param rolename 角色名称
     * @return 增加条数，失败则返回0，成功增加1条返回1
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/addRoleInf")
    public String addRoleInf(HttpServletRequest request,String rolecode,String rolename,Model model){
        int result = 0;
        if(SecurityTool.isReturnJson("1",request)){
            result = managerInfDao.addRoleInf(rolecode,rolename);
        }
        model.addAttribute("result",result);
        return Constant.JSON_VIEW_OTHER;
    }

    /**
     * localhost:8888/usermanager/delManager.action 删除人员信息
     * @param manname 管理者帐号名称(不是实际名称realname)
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/delManager")
    public String delManager(HttpServletRequest request,String manname,Model model){
        int result=0;
        if(SecurityTool.isReturnJson("1",request)){
            result = managerInfDao.delManger(manname);
        }
        model.addAttribute("result",result);
        return Constant.JSON_VIEW_OTHER;
    }

    /**
     *  localhost:8888/usermanager/updateManagerInf.action 更改管理用户信息
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/updateManagerInf")
    public String updateManagerInf(HttpServletRequest request,Model model){
        String manname = request.getParameter("manname");
        if(manname!=null){
            if(SecurityTool.isReturnJson("1",request)){
                ManagerEntity m = managerDao.getManagerByManname(manname);
                ManagerInfEntity mi = managerInfDao.getManagerInfByManname(manname);
                String realname = null!=request.getParameter("realname")?request.getParameter("realname"):mi.getRealname();
                //修改角色不允许将名称设为空。
                String manphone = request.getParameter("manphone");
                String manrole = request.getParameter("manrole");
                String permission = request.getParameter("manpermission");
                mi.setManphone(manphone);
                mi.setRealname(realname);
                mi.setManrole(manrole);
                m.setManpermission(permission);
                managerInfDao.updateManagerInf(mi,m);
            }
        }
        model.addAttribute("result",true);
        return Constant.JSON_VIEW_OTHER;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/addManager")
    public String addManager(HttpServletRequest request,Model model){
        String manname = request.getParameter("manname");
        String msg = "错误,manname为空!";
        if(manname!=null){
            if(SecurityTool.isReturnJson("1",request)){
                ManagerEntity m = new ManagerEntity();
                ManagerInfEntity mi = new ManagerInfEntity();
                String realname = request.getParameter("realname");
                realname = (realname!=null)?realname:mi.getRealname();
                String manphone = request.getParameter("manphone");
                String manrole = request.getParameter("manrole");
                String permission = request.getParameter("manpermission");
                String password = request.getParameter("manpassword");
                mi.setManname(manname);
                mi.setManphone(manphone);
                mi.setRealname(realname);
                mi.setManrole(manrole);
                m.setManpermission(permission);
                m.setManpassword(password);
                managerInfDao.addManager(mi,m);
                msg="ok";
            }
        }
        model.addAttribute("msg",msg);
        return Constant.JSON_VIEW_OTHER;
    }

    /**
     * localhost:8888/usermanager/addUserInf.action 增加用户信息
     * @param request 客户信息，如{username:xxx,realname:xxx,同数据库,另外如果id为空,id会用UUID随机生成}
     * @return true/false
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/addUserInf")
    public String addUserInf(HttpServletRequest request,Model model,String username,String realname,
                             String useradd,String useremail,String userphone,String usercard){
        if(username!=null){
            if(SecurityTool.isReturnJson("1",request)){
                UserInfEntity user = new UserInfEntity();
                user.setUsername(username);
                user.setRealname(realname);
                user.setUseradd(useradd);
                user.setUseremail(useremail);
                user.setUserphone(userphone);
                user.setUsercard(usercard);
                userInfDao.addAllUserInf(user);
            }
        }
        model.addAttribute("result",true);
        return Constant.JSON_VIEW_OTHER;
    }

    @RequestMapping("/test")
    public void test(HttpServletRequest request,HttpServletResponse response){
        try {
            //根据html指定的jsonp回调函数的参数名，获取回调函数的名称
            String callbackName=request.getParameter("jsoncallback");
            //callbackName的值其实就是：jsonpCallback_success
            System.out.println("---------"+callbackName);
            List userList = null;
            if(SecurityTool.isReturnJson("1",request)){
                userList = userInfDao.getAllUserInf();
            }
            String result = JSON.toJSONString(userList);
            //最终返回的数据为
            String renderStr=callbackName+"("+result+")";
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(renderStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/test2")
    public String test2(HttpServletRequest request,Model model){
        List userList = null;
        if(SecurityTool.isReturnJson("1",request)){
            userList = userInfDao.getAllUserInf();
        }
        model.addAttribute("userList",userList);
        return Constant.JSON_VIEW_OTHER;
    }

}
